"""
---------------------------------------------------------------------
- File: logs.py
- Date: 2025/7/26
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: api 日志表
---------------------------------------------------------------------
"""

from tortoise import models, fields


class Logs(models.Model):
    """API 请求日志模型"""

    id = fields.BigIntField(pk=True, description="日志ID")
    request_id = fields.CharField(max_length=255, unique=True, description="请求唯一ID")

    # -- 时间与性能 --
    created_at = fields.DatetimeField(
        auto_now_add=True, unique=True, description="请求时间"
    )
    processing_time_ms = fields.IntField(description="耗时（毫秒）")

    # -- 请求来源 --
    client_ip = fields.CharField(max_length=100, null=True, description="客户端IP")
    user_no = fields.CharField(max_length=100, null=True, description="用户编号")
    user_name = fields.CharField(max_length=100, null=True, description="用户名称")

    # -- 请求信息 --
    path = fields.CharField(max_length=255, description="请求路径")
    method = fields.CharField(max_length=10, description="请求方法")
    request_headers = fields.JSONField(null=True, description="请求头")
    query_params = fields.JSONField(null=True, description="查询参数")
    request_body = fields.JSONField(null=True, description="请求体")

    # -- 响应信息 --
    status_code = fields.IntField(description="响应状态码")
    response_body = fields.JSONField(null=True, description="响应体")

    class Meta:
        table = "logs"
        table_description = "API 请求日志表"
        ordering = ["-created_at"]
